package com.bianxingji.api;


import com.bianxingji.model.RestResponse;
import com.bianxingji.service.BxjMediaFileService;
import io.minio.MinioClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Mr.M
 * @version 1.0
 * @description 上传视频
 * @date 2023/2/18 10:34
 */

@Api(value = "大文件上传接口", tags = "大文件上传接口")
@RestController
@RequestMapping("/bigMedia")
public class BigFilesController {

    @Autowired
    BxjMediaFileService mediaFileService;

    @Autowired
    private MinioClient minioClient;

    @ApiOperation(value = "文件上传前检查文件")
    @PostMapping("/upload/checkfile")
    public RestResponse<Boolean> checkfile(
            @RequestParam("fileMd5") String fileMd5
    ) throws Exception {
        RestResponse<Boolean> booleanRestResponse = mediaFileService.checkFile(fileMd5);
        return booleanRestResponse;
    }


//
//    @GetMapping("/upload/test")
//    public String test()  {
//        File file = new File("C:\\Users\\24478\\Videos\\5d1fbc0d-75b7-4afa-b101-f60a90f4fdc8.mp4");
//        String extension = file.getName().substring(file.getName().lastIndexOf("."));
//        ContentInfo extensionMatch = ContentInfoUtil.findExtensionMatch(extension);
//        String mimeType = MediaType.APPLICATION_OCTET_STREAM_VALUE;//通用mimeType，字节流
//        if(extensionMatch!=null){
//            mimeType = extensionMatch.getMimeType();
//        }
//        UploadObjectArgs uploadObjectArgs = null;
//        try {
//            uploadObjectArgs = UploadObjectArgs.builder()
//                    .bucket("mediafiles")//桶
//                    .filename(file.getAbsolutePath()) //指定本地文件路径
//                    .object("6/6/"+"5d1fbc0d-75b7-4afa-b101-f60a90f4fdc8.mp4")//对象名 放在子目录下
//                    .contentType(mimeType)//设置媒体文件类型
//                    .build();
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }
//        //上传文件
//        try {
//            minioClient.uploadObject(uploadObjectArgs);
//        } catch (Exception e) {
//            throw new RuntimeException(e);
//        }
//        return "OK";
//    }


//
    @ApiOperation(value = "分块文件上传前的检测")
    @PostMapping("/upload/checkchunk")
    public RestResponse<Boolean> checkchunk(@RequestParam("fileMd5") String fileMd5,
                                            @RequestParam("chunk") int chunk) throws Exception {
        RestResponse<Boolean> booleanRestResponse = mediaFileService.checkChunk(fileMd5,chunk);
        return booleanRestResponse;
    }

//    @ApiOperation(value = "上传分块文件")
//    @PostMapping("/upload/uploadchunk")
//    public RestResponse uploadchunk(@RequestParam("file") MultipartFile file,
//                                    @RequestParam("fileMd5") String fileMd5,
//                                    @RequestParam("chunk") int chunk) throws Exception {
//
//        //创建一个临时文件
//        File tempFile = File.createTempFile("minio", ".temp");
//        file.transferTo(tempFile);
//        //文件路径
//        String localFilePath = tempFile.getAbsolutePath();
//
//        RestResponse restResponse = mediaFileService.uploadChunk(fileMd5, chunk, localFilePath);
//
//        return restResponse;
//    }
//
//    @ApiOperation(value = "合并文件")
//    @PostMapping("/upload/mergechunks")
//    public RestResponse mergechunks(@RequestParam("fileMd5") String fileMd5,
//                                    @RequestParam("fileName") String fileName,
//                                    @RequestParam("chunkTotal") int chunkTotal) throws Exception {
//        Long companyId = 1232141425L;
//        //文件信息对象
//        UploadFileParamsDto uploadFileParamsDto = new UploadFileParamsDto();
//        uploadFileParamsDto.setFilename(fileName);
//        uploadFileParamsDto.setTags("视频文件");
//        uploadFileParamsDto.setFileType("001002");
//        RestResponse restResponse = mediaFileService.mergechunks(1232141425L, fileMd5, chunkTotal, uploadFileParamsDto);
//        return restResponse;
//
//    }
//

}
